# Joy -- implementation of the Joy programming language
# Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
#
# Joy is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# Joy is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Joy.  If not, see <http://www.gnu.org/licenses/>.

# The `:;' works around a Bash 3.2 bug when the output is not writable.
$(srcdir)/tests/package.m4: $(top_srcdir)/configure.ac
	$(AM_V_GEN):;{ \
	  echo '# Signature of the current package.' && \
	  echo 'm4_define([AT_PACKAGE_NAME],' && \
	  echo '  [$(PACKAGE_NAME)])' && \
	  echo 'm4_define([AT_PACKAGE_TARNAME],' && \
	  echo '  [$(PACKAGE_TARNAME)])' && \
	  echo 'm4_define([AT_PACKAGE_VERSION],' && \
	  echo '  [$(PACKAGE_VERSION)])' && \
	  echo 'm4_define([AT_PACKAGE_STRING],' && \
	  echo '  [$(PACKAGE_STRING)])' && \
	  echo 'm4_define([AT_PACKAGE_BUGREPORT],' && \
	  echo '  [$(PACKAGE_BUGREPORT)])'; \
	  echo 'm4_define([AT_PACKAGE_URL],' && \
	  echo '  [$(PACKAGE_URL)])'; \
	} >'$@'

TESTSUITE = $(srcdir)/testsuite

check-local: tests/atconfig $(TESTSUITE) $(top_builddir)/pre-inst-env
	$(AM_V_at)$(top_builddir)/pre-inst-env $(SHELL) '$(TESTSUITE)' -C tests \
	  $(filter -j%,$(MAKEFLAGS)) $(TESTSUITEFLAGS)

installcheck-local: tests/atconfig $(TESTSUITE)
	$(AM_V_at)$(SHELL) '$(TESTSUITE)' -C tests AUTOTEST_PATH='$(bindir)' \
	  $(filter -j%,$(MAKEFLAGS)) $(TESTSUITEFLAGS)

clean-local:
	test ! -f '$(TESTSUITE)' || \
	 $(SHELL) '$(TESTSUITE)' --clean

AUTOM4TE = $(SHELL) $(srcdir)/build-aux/missing --run autom4te
AUTOTEST = $(AUTOM4TE) --language=autotest

# Files included by testsuite.at
TEST_MODULES = \
  tests/joy-base.at \
  tests/joy-forth.at \
  tests/joy-repl.at \
  tests/joy.at \
  $(EMPTY)

# Joy source used by test modules
TEST_JOYS = \
  tests/inicheck.joy \
  tests/test-base.joy \
  tests/test-forth.joy \
  $(EMPTY)

$(TESTSUITE): $(srcdir)/tests/testsuite.at $(srcdir)/tests/local.at $(srcdir)/tests/package.m4 $(TEST_MODULES)
	$(AM_V_GEN)$(AUTOTEST) -I '$(srcdir)/tests' -o $@.tmp $<
	$(AM_V_at)mv $@.tmp $@

tests/atconfig: $(top_builddir)/config.status
	cd $(top_builddir) && $(SHELL) ./config.status $@

EXTRA_DIST += \
  tests/testsuite.at \
  tests/local.at \
  $(TESTSUITE) tests/atlocal.in \
  $(TEST_MODULES) \
  $(TEST_JOYS) \
  $(EMPTY)

CLEANFILES += \
  tests/testsuite.log \
  $(EMPTY)

DISTCLEANFILES += \
  tests/atconfig \
  $(EMPTY)
